package com.service;

import com.dao.MessageDao;
import com.entity.Message;
import com.util.LocalInformation;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by wlc on 2018/5/6.
 */
public class MessageOperator {
    /**
     * 发送消息到指定用户
     * @param projectPath
     * @param message
     * @throws IOException
     */
    public static void sendMessage(String projectPath,Message message) throws IOException{
        String messagePath = projectPath + "/file_data/message/" + message.getReceiverId() + "/" + message.getId() + "/" + message.getId() + ".txt";
        message.setReadStatus("0");
        MessageDao.saveMessage(messagePath,message);
    }

    /**
     * 获取指定用户目录下的消息数目
     * @param projectPath
     * @param userId
     * @return
     */
    public static String getMessageNum(String projectPath, String userId){
        //       ../id/messageId.txt
        String messageDir = projectPath + "/file_data/message/" + userId + "/";

        return MessageDao.getMessageFileNum(messageDir);
    }

    /**
     * 获取指定用户目录下的所有消息信息
     * @param projectPath
     * @param userId
     * @return
     */
    public static ArrayList<Message> getAllMessageInfo(String projectPath, String userId) throws IOException{
        String messageDir = projectPath + "/file_data/message/" + userId + "/";

        return MessageDao.getAllMessageContent(messageDir);
    }

    public static Message getMessageById(String projectPath, String userId, String messageId)throws IOException{
        String messagePath = projectPath + "/file_data/message/" + userId + "/" + messageId + "/" + messageId + ".txt";
        return MessageDao.getMessageFile(messagePath);
    }

    /**
     * 由messageId获取图片附件名
     * @param projectPath
     * @param userId
     * @param messageId
     * @return
     */
    public static ArrayList<String> getMessageImageById(String projectPath, String userId, String messageId) {
        ArrayList<String> imageName = new ArrayList<>();
        String messageDir = projectPath + "/file_data/message/" + userId + "/" + messageId + "/";

        ArrayList<File> imgFile = MessageDao.getImageFile(messageDir);
        if(imgFile != null) {
            for(File file: imgFile) {
                String fileName =  file.getName();
                imageName.add(fileName);
            }
        }
        return imageName;
    }



    /**
     * 删除所给ID数组中的所有消息
     * @param projectPath
     * @param userId
     * @param messageIds
     */
    public static void delMessages(String projectPath, String userId,ArrayList<String> messageIds){
        String messagePath = projectPath + "/file_data/message/" + userId + "/";
        for (String messageId : messageIds){
            MessageDao.delMessageFile(messagePath+messageId);
        }

    }

    /**
     * 标记所给ID数组中所有消息为已读
     * @param projectPath
     * @param userId
     * @param messageIds
     */
    public static void markMessagesRead(String projectPath, String userId,ArrayList<String> messageIds) throws IOException{
        String messagePath = projectPath + "/file_data/message/" + userId + "/";

        //这边是每次+=path会叠加？
        for (String messageId : messageIds){
            String tmpPath = messagePath;
            tmpPath += messageId + "/" + messageId + ".txt";
            MessageDao.markMessageFileRead(tmpPath);
        }

    }
}
